using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class BattleAttackBar : MonoBehaviour
{
	public BattlePartyMember TargetPartyMember;

	public BattleActiveEnemy TargetActiveEnemy;

	[SerializeField]
	private Image BarMemberIcon;

	[SerializeField]
	private RawImage AttackBarBackground;

	public RectTransform AttackBar;

	[SerializeField]
	private RectTransform CriticalPosition;

	public bool HasSetupPosition;

	public bool IsCurrentlyCritical;

	private void Update()
	{
		if (((Component)this).gameObject.activeSelf && HasSetupPosition)
		{
			UpdateBarPosition();
			if (CalculateAccuracy() == 150f)
			{
				IsCurrentlyCritical = true;
			}
			else
			{
				IsCurrentlyCritical = false;
			}
		}
	}

	public void PlayPartyMemberAttackAnimation()
	{
		if (TargetPartyMember != null)
		{
			BattleSystem.Instance.PartyMembers_MemberPlayAnimation(TargetPartyMember, "Attack");
		}
	}

	public float CalculateDamage(int Attack, float Accuracy, int EnemyDefense)
	{
		EnemyDefense = Mathf.Clamp(EnemyDefense, 1, int.MaxValue);
		return Mathf.Clamp((float)Attack * Accuracy / 20f - (float)(3 * EnemyDefense), 0f, float.PositiveInfinity);
	}

	public float CalculateAccuracy()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		float num = Vector2.Distance(CriticalPosition.anchoredPosition, AttackBar.anchoredPosition);
		if (num <= 8f)
		{
			AttackBar.anchoredPosition = new Vector2(-465.7925f, AttackBar.anchoredPosition.y);
			return 150f;
		}
		if (num <= 10f)
		{
			return 120f;
		}
		if (num <= 20f)
		{
			return 110f;
		}
		return 100f;
	}

	private void UpdateBarPosition()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		AttackBar.anchoredPosition = new Vector2(AttackBar.anchoredPosition.x - 468f * Time.deltaTime, AttackBar.anchoredPosition.y);
	}

	public void UpdateGraphics()
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		if (TargetPartyMember != null)
		{
			BarMemberIcon.sprite = TargetPartyMember.PartyMemberInBattle.PartyMemberBattleIcon;
			((Graphic)AttackBarBackground).color = TargetPartyMember.PartyMemberInBattle.PartyMemberColor;
		}
	}

	public float GetDistanceToCritical()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		return Vector2.Distance(CriticalPosition.anchoredPosition, AttackBar.anchoredPosition);
	}

	public void SetNewBarPosition(float NewX)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		MonoBehaviour.print((object)"setup new bar positon");
		AttackBar.anchoredPosition = new Vector2(NewX, AttackBar.anchoredPosition.y);
		HasSetupPosition = true;
	}

	public void FadeAttackBar(float fadeSpeed = 1f, Color targetColor = default(Color))
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		((MonoBehaviour)this).StartCoroutine(FadeAttackBarTimed(fadeSpeed, targetColor));
	}

	private IEnumerator FadeAttackBarTimed(float fadeSpeed, Color targetColor = default(Color))
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		Color barcolor = ((Graphic)((Component)AttackBar).GetComponent<RawImage>()).color;
		while (barcolor != targetColor)
		{
			barcolor = Color.Lerp(barcolor, targetColor, fadeSpeed * Time.deltaTime);
			((Graphic)((Component)AttackBar).GetComponent<RawImage>()).color = barcolor;
			if (Vector4.Distance(Color.op_Implicit(barcolor), Color.op_Implicit(targetColor)) < 0.01f)
			{
				((Graphic)((Component)AttackBar).GetComponent<RawImage>()).color = targetColor;
				break;
			}
			yield return null;
		}
	}
}
